package com.xiezc.model;

import lombok.Getter;
import lombok.Setter;

/**
 * 每条消息的发送进度的实时更新
 *
 * @author wb-xzc291800
 * @date 2018/09/18 17:30
 */

public class SendProgress {

    /**
     * 消息的ID
     */
    @Setter
    @Getter
    private long msgId;

    /**
     * 消息拆分包的总个数
     */
    @Setter
    @Getter
    private long len;

    /**
     * 消息确认发送成功的包的个数
     */
    private long sendCount;

    /**
     * 消息成功还是失败
     * null  发送中, false发送失败, true 发送成功
     *
     */
    @Setter
    @Getter
    private Boolean success;

    /**
     * 消息开始的发送时间
     */
    @Setter
    @Getter
    private long startTime;

    /**
     * 消息结束的时间
     */
    @Setter
    @Getter
    private long endTime;

    public synchronized void sendCountAddOne() {

        sendCount += 1;
        if (sendCount == len) {
            success = true;
            endTime = System.currentTimeMillis();
        }
    }

    public synchronized long getSendCount() {

        return sendCount;
    }

    @Override
    public boolean equals(Object o) {

        if (this == o) { return true; }
        if (!(o instanceof SendProgress)) { return false; }

        SendProgress that = (SendProgress)o;

        return getMsgId() == that.getMsgId();
    }

    @Override
    public int hashCode() {

        return (int)(getMsgId() ^ (getMsgId() >>> 32));
    }
}
